﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using blackhouse.WebNemo.Business.Groups;
using mojoPortal.Business;
using mojoPortal.Business.WebHelpers;
using mojoPortal.Web;
using blackhouse.BlackApart.Web;

namespace blackhouse.Aparts.Web.Nemo {
    public class EventExtension : RecordExtension {

        public EventExtension(GroupRecord record) : base(record) { }

        protected override string GetIdentyficator(GroupRecord record) {
            return record.GetString("tytul");
        }

        protected override void RecordChanged(GroupRecord record, RecordChangeType type) {
            string url = "~/" + record.GetString("tytul").CleanStringForUrl().RemovePolishChars().ToLower();

            int siteId = 1;
            Guid siteGuid = Guid.Empty;

            SiteSettings ss = CacheHelper.GetCurrentSiteSettings();
            if (ss != null) {
                siteId = CacheHelper.GetCurrentSiteSettings().SiteId;
                siteGuid = ss.SiteGuid;
            }

            string newUrl = url.Replace("~/", string.Empty);
            FriendlyUrl friendlyUrl = new FriendlyUrl(siteId, newUrl);

            if (!friendlyUrl.FoundFriendlyUrl) {
                if (WebPageInfo.IsPhysicalWebPage("~/" + url)) {
                    return;
                }
            }

            string oldUrl = record.GetString("url");
            if (!string.IsNullOrEmpty(oldUrl) && oldUrl.StartsWith("~/"))
                oldUrl = oldUrl.Replace("~/", string.Empty);
            record.SetValue("url", newUrl);
            this.SaveRecord();

            if (!friendlyUrl.FoundFriendlyUrl) {
                if ((url.Length > 0) && (!WebPageInfo.IsPhysicalWebPage("~/" + url))) {
                    FriendlyUrl newFriendlyUrl = new FriendlyUrl();
                    newFriendlyUrl.SiteId = siteId;
                    newFriendlyUrl.SiteGuid = siteGuid;
                    newFriendlyUrl.PageGuid = CacheHelper.GetCurrentPage().PageGuid;
                    newFriendlyUrl.Url = newUrl;
                    newFriendlyUrl.RealUrl = UrlUtils.LocalizeUrl("/Pages/Events.aspx?eventId=" + record.RecordId);

                    newFriendlyUrl.Save();
                }

                //if post was renamed url will change, if url changes we need to redirect from the old url to the new with 301
                if ((oldUrl.Length > 0) && (newUrl.Length > 0) && (!SiteUtils.UrlsMatch(oldUrl, newUrl))) {
                    //worry about the risk of a redirect loop if the page is restored to the old url again
                    // don't create it if a redirect for the new url exists
                    if (
                        (!RedirectInfo.Exists(siteId, oldUrl))
                        && (!RedirectInfo.Exists(siteId, newUrl))
                        ) {
                        RedirectInfo redirect = new RedirectInfo();
                        redirect.SiteGuid = siteGuid;
                        redirect.SiteId = siteId;
                        redirect.OldUrl = oldUrl;
                        redirect.NewUrl = newUrl;
                        redirect.Save();
                    }
                    // since we have created a redirect we don't need the old friendly url
                    FriendlyUrl oldFriendlyUrl = new FriendlyUrl(siteId, oldUrl);
                    if (oldFriendlyUrl.FoundFriendlyUrl) {
                        FriendlyUrl.DeleteUrl(oldFriendlyUrl.UrlId);
                    }

                }
            }

        }

        protected override void RecordTranslated(GroupRecord record, GroupColumn column, string prevValue, string value) { }

    }
}
